package wsi.gui;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.KeyStroke;

import org.rygh.gui.model.IHasSLabel;
import org.rygh.gui.swing.button.SButton;
import org.rygh.gui.swing.component.SToolBar;
import org.rygh.gui.swing.menu.SExitMenuItem;
import org.rygh.gui.swing.menu.SMenu;
import org.rygh.gui.swing.menu.SMenuBar;
import org.rygh.gui.swing.menu.SMenuItem;
import org.rygh.gui.swing.presenter.SwingPresenter;
import org.rygh.gui.swing.view.SMainView;
import org.rygh.gui.swing.view.one.SOneHtmlView;
import org.rygh.gui.swing.view.one.SOneXmlView;
import org.rygh.gui.swing.window.SMainWindow;

import wsi.function.Function;
import wsi.script.AppConfig;

@SuppressWarnings("serial")
public class AppWindow extends SMainWindow implements AppPresenter.Display {

	private final SMainView view = new SMainView();
	private final SOneHtmlView htmlView = new SOneHtmlView();
	private final SOneXmlView xmlView = new SOneXmlView();
	private final SwingPresenter presenter;
	private final SMenu functionMenu = new SMenu("Functions", KeyEvent.VK_T);

	public AppWindow(SwingPresenter presenter) {
		super(AppConfig.shared().getRunsFolder().getName()
				+ " - Web Service Integration");

		this.presenter = presenter;

		layoutComponents();
		setLocation(100, 60);
	}

	private SMenuBar createMenuBar() {
		SMenuBar bar = new SMenuBar();
		SMenuItem item;

		SMenu menu = new SMenu("File", KeyEvent.VK_F);
		menu.add(new SMenuItem("New...", KeyEvent.VK_N, presenter, "newText"));
		menu.addSeparator();
		menu.add(new SMenuItem("Save...", KeyEvent.VK_N, presenter, "saveML"));
		menu.addSeparator();
		menu.add(new SMenuItem("Preferences...", KeyEvent.VK_P, presenter,
				"preferences"));
		menu.addSeparator();
		menu.add(new SMenuItem("Jena Preferences...", IHasSLabel.NO_MNEMONIC,
				presenter, "jenaPreferences"));
		menu.addSeparator();
		menu.add(new SExitMenuItem(presenter));
		bar.add(menu);
		bar.add(functionMenu);

		menu = new SMenu("Operation", KeyEvent.VK_O);
		item = new SMenuItem("Launch...", KeyEvent.VK_L, presenter, "launch");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
				ActionEvent.CTRL_MASK));
		menu.add(item);
		menu.addSeparator();
		item = new SMenuItem("Reset Parameters", KeyEvent.VK_R, presenter,
				"resetParameters");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,
				ActionEvent.CTRL_MASK));
		menu.add(item);

		item = new SMenuItem("Prepare Inputs...", KeyEvent.VK_I, presenter,
				"prepareInputs");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
				ActionEvent.CTRL_MASK));
		menu.add(item);
		bar.add(menu);

		menu = new SMenu("Database", KeyEvent.VK_D);
		menu.add(new SMenuItem("Query...", KeyEvent.VK_Q, presenter, "query"));
		menu.add(new SMenuItem("Save...", KeyEvent.VK_S, presenter,
				"saveDatabase"));
		menu.addSeparator();
		item = new SMenuItem("Inject...", KeyEvent.VK_I, presenter, "inject");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
				ActionEvent.CTRL_MASK));
		menu.add(item);
		item = new SMenuItem("View...", KeyEvent.VK_E, presenter,
				"viewDatabase");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
				ActionEvent.CTRL_MASK));
		menu.add(item);
		menu.add(new SMenuItem("Clear", KeyEvent.VK_C, presenter,
				"clearDatabase"));
		menu.addSeparator();
		menu.add(new SMenuItem("View Xml...", IHasSLabel.NO_MNEMONIC,
				presenter, "viewXml"));
		menu.addSeparator();
		menu.add(new SMenuItem("Predicate Path Merge...", KeyEvent.VK_M,
				presenter, "mergePredicatePath"));
		menu.add(new SMenuItem("Predicate Path Time", IHasSLabel.NO_MNEMONIC,
				presenter, "viewPredicatePathTime"));
		menu.add(new SMenuItem("Predicate Path Check", IHasSLabel.NO_MNEMONIC,
				presenter, "checkPredicatePathTime"));
		bar.add(menu);

		menu = new SMenu("Experiment", KeyEvent.VK_E);
		menu.add(new SMenuItem("No. 1 - Select Tree", IHasSLabel.NO_MNEMONIC,
				presenter, "experiemnt1"));
		menu.add(new SMenuItem("No. 2 - Tree to Entity Match",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt2"));
		menu.add(new SMenuItem("No. 2 - Tree to Instance Match",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt2b"));
		menu.add(new SMenuItem("No. 3 - Path Match (by pair)",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt3Pair"));
		menu.add(new SMenuItem("No. 3 - Path Match (by predicate)",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt3Predicate"));
		menu.add(new SMenuItem("No. 4 - Web Call Ratio",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt4"));
		menu.add(new SMenuItem("No. 6 - Functional Dependency",
				IHasSLabel.NO_MNEMONIC, presenter, "experiemnt6"));
		bar.add(menu);

		menu = new SMenu("Help", KeyEvent.VK_H);
		menu.add(new SMenuItem("Temporary Task", KeyEvent.VK_A, presenter,
				"tempTask"));
		menu.addSeparator();
		menu.add(new SMenuItem("About", KeyEvent.VK_A, presenter, "about"));
		bar.add(menu);

		return bar;
	}

	private SToolBar createToolBar() {
		SToolBar bar = new SToolBar();

		bar.add(new SButton("Launch", KeyEvent.VK_L, presenter, "launch"));
		bar.addSeparator();

		bar.add(new SButton("Query", KeyEvent.VK_Q, presenter, "query"));
		bar.addSeparator();

		bar.add(new SButton("Reset", KeyEvent.VK_R, presenter,
				"resetParameters"));
		bar.addSeparator();

		// bar.add(new Button("Inject", KeyEvent.VK_I, presenter, "inject"));
		// bar.add(new Button("Save", KeyEvent.VK_S, presenter,
		// "saveDatabase"));
		bar.add(new SButton("View", KeyEvent.VK_V, presenter, "viewDatabase"));
		bar.add(new SButton("Clear", KeyEvent.VK_C, presenter, "clearDatabase"));

		return bar;
	}

	@Override
	public SMenu getFunctionMenu() {
		return functionMenu;
	}

	private void layoutComponents() {
		view.setLayout(new CardLayout());
		view.setPreferredSize(new Dimension(640, 480));

		view.add(htmlView, "HTML");
		view.add(xmlView, "XML");

		setJMenuBar(createMenuBar());
		addTopView(createToolBar());
		addCenterView(view);

		pack();
	}

	@Override
	public void setHtml(String html) {
		htmlView.setHtml(html);

		((CardLayout) view.getLayout()).show(view, "HTML");
	}

	@Override
	public void setXml(String xml) {
		xmlView.setXml(xml);

		((CardLayout) view.getLayout()).show(view, "XML");
	}

	@Override
	public void updateFunctionMenu() {
		functionMenu.removeAll();

		List<Function> functions = Property.shared().getLastestFunctions();
		SMenuItem item;

		if (functions.isEmpty()) {
			item = new SMenuItem("N/A");
			item.setEnabled(false);
			functionMenu.add(item);
		} else {
			for (Function f : functions) {
				item = new SMenuItem(f.getFullName());
				item.setValue(f);
				functionMenu.add(item);
			}
		}
	}
}
